package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.Acoustic;
import com.itheima.mapper.AcousticMapper;
import com.itheima.service.db.AcousticService;
import com.itheima.util.RamdomUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@DubboService
public class AcousticServiceImple implements AcousticService {

    @Autowired
    private AcousticMapper acousticMapper;

    //添加语音
    @Override
    public void sendAcoustic(Long id, String webServerUrl) {
        //封装
        Acoustic acoustic = new Acoustic();

        acoustic.setUserId(id);
        acoustic.setAddress(webServerUrl);
         acousticMapper.insert(acoustic);
    }


    @Override
    public Acoustic findAcoustic(Long userId) {

        //查询 其他人发送的信息
        LambdaQueryWrapper<Acoustic> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(Acoustic::getUserId, userId);
        lambdaQueryWrapper.eq(Acoustic::getPickup, 0);
        lambdaQueryWrapper.orderByAsc(Acoustic::getCreated);
        acousticMapper.selectList(lambdaQueryWrapper);
        List<Acoustic> records = acousticMapper.selectList(lambdaQueryWrapper);;
        Integer[] integers = RamdomUtil.randomCommon(0, records.size() - 1, 1);
        Integer a=0;
        for (Integer integer : integers) {
            a = integer;
        }


        return records.get(a);

    }
    //修改
    @Override
    public void update(Acoustic acoustic) {

        acousticMapper.updateById(acoustic);
    }
    //查询 改用户今天拾取的次数
    @Override
    public Integer findCount(Long userId) {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        Date startTime = todayStart.getTime();

        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        Date endtime = todayEnd.getTime();


        LambdaQueryWrapper<Acoustic> wq = new LambdaQueryWrapper<>();
        wq.eq(Acoustic::getPickup, 1);
        wq.eq(Acoustic::getPickupUserId, userId);
        wq.between(Acoustic::getPickupDateTime, startTime, endtime);
        Integer count = acousticMapper.selectCount(wq);
        return count;
    }

}
